package com.shd.edanpei.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;

import com.jaeger.library.StatusBarUtil;
import com.shd.edanpei.R;
import com.shd.edanpei.base.dialog.Loader;
import com.shd.edanpei.util.AcCollector;
import com.shd.edanpei.image.LoadImageCtrl;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class TranBarActivity<P extends BaseIPresenter> extends AppCompatActivity
        implements BaseIView {


    private Unbinder mUnbinder;
    protected P mPresenter;

    /**
     * 加载图片控制类
     */
    protected LoadImageCtrl loadImageCtrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置activity为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //设置状态栏字体颜色
        StatusBarUtil.setDarkMode(this);
        //设置布局
        setContentView(setLayout());
        //绑定ButterKnife
        mUnbinder = ButterKnife.bind(this);
        //绑定Presenter
        mPresenter = bindPresenter();

        //初始化
        initView(savedInstanceState);
        //监听
        initListener();
        //将activity加入控制器
        AcCollector.addActivity(this);
    }

    /**
     * 设置布局ID
     *
     * @return
     */
    protected abstract int setLayout();

    /**
     * 绑定Presenter
     *
     * @return
     */
    protected abstract P bindPresenter();

    /**
     * 初始化
     *
     * @param savedInstanceState
     */
    protected abstract void initView(@Nullable Bundle savedInstanceState);

    /**
     * 初始化监听
     */
    protected abstract void initListener();


    @Override
    public void showLoading(@Nullable String str) {
        Loader.show(getContext(), str);
    }

    @Override
    public void stopLoading() {
        Loader.stop();
    }


    @Override
    public FragmentActivity getContext() {
        return this;
    }

    @Override
    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    @Override
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @Override
    public void viewAnim(View view) {
        Animation shake = AnimationUtils.loadAnimation(getContext(), R.anim.shake);
        view.startAnimation(shake);
    }

    @Override
    public boolean etData(EditText... views) {
        for (int i = 0; i < views.length; i++) {
            if (TextUtils.isEmpty(views[i].getText().toString())) {
                viewAnim(views[i]);
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return false;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (loadImageCtrl != null) {
            loadImageCtrl.onStart();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (loadImageCtrl != null) {
            loadImageCtrl.onStop();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mUnbinder) {
            mUnbinder.unbind();
            mUnbinder = null;
        }
        if (loadImageCtrl != null) {
            loadImageCtrl.onDestroy();
        }
        AcCollector.removeActivity(this);
    }

}
